<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = new fenAjax("Formulaire texte",0);

if(isset($_POST['mode']) and isset($_POST['id_texte']) and isset($_POST['statut']) and isset($_POST['contenu'])) {
	
	$mode = $_POST['mode'];
	$id_texte = $_POST['id_texte'];
	$statut = $_POST['statut'];
	$contenu = $_POST['contenu'];
	
	$id = $_SESSION['user_id'];
	
	
	// mode 1 : suppression admin
	// mode 2 : modification admin
	// mode 3 : suppression utilisateur
	// mode 4 : modification user
	
	if( !in_array($mode,array(1,2,3,4)))
		$f->ajaxErreur("Fonction non supportée","Ce mode n'est pas supporté pour ce module");
		
	if($mode == 1 or $mode ==3){		$header = "Selectionner le texte"; 		$nomAction = "Supprimer";		$disable = ""; }
	if($mode == 2 or $mode ==4){		$header = "Modifier le texte"; 			$nomAction = "Modifier";		$disable = ""; }
		
	$f->retourAjax["boutton"] = $nomAction;
	$f->retourAjax["header"] = $header;
	
	$form = "<div id=\"feedback\"></div><br />";
	
	if ($mode == 1 or $mode == 3) {
		$form .= BMInfo("Note: La suppression d'un texte entraine la suppression de tout ses commentaires",false);
	}
	
	$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionTexte($(this));\">";
	
	
	if ($mode == 1 or $mode == 3) {
		
		$form .= "<br /><label class=\"formInfos\" for=\"id_texte\">Texte :</label>
					<select id=\"id_texte\" name=\"id_texte\" $disable>";
		
		if ($mode == 1) {
			$res = mysql_query("SELECT * FROM Texte ORDER BY titre;");
		}
		
		if ($mode == 3) {
			$res = mysql_query("SELECT * FROM PubliUserTexte WHERE idUser = ".$id.";");  //Retourne les différents id de texte
		}
		
		if ($mode == 1) {
		
			$prems = true;
		
			while($row = mysql_fetch_array($res)) {
				
				// Afffiche tout les textes valides
				
				if ($row['statut'] != "0" and $row['statut'] != "4") {
					if ($prems) {
						$form .= "<option value=\"".$row['id']."\" selected=\"selected\">".$row['titre']." de ".$row['auteur']."</option>
						";
						$prems = false;
					}
					else {
						$form .= "<option value=\"".$row['id']."\" >".$row['titre']." de ".$row['auteur']."</option>
						";
					}
				}
			}
		}
		
		
		if ($mode == 3) {
		
			$prems = true;
			
			while($row = mysql_fetch_array($res)) {
				$res2 = mysql_query("SELECT * FROM Texte WHERE id = ".$row['idTexte']." ORDER BY titre;");
				
				while($row2 = mysql_fetch_array($res2)) {
				
					// Afffiche tout les textes de l'utilisateur
					
					/* if ($row2['statut'] != "0") { */   // Ne proposerais de supprimer que les textes valide
						if ($prems) {
							$form .= "<option value=\"".$row2['id']."\" selected=\"selected\">".$row2['titre']." de ".$row2['auteur']."</option>
							";
							$prems = false;
						}
						else {
							$form .= "<option value=\"".$row2['id']."\" >".$row2['titre']." de ".$row2['auteur']."</option>
							";
						}
					/* } */
				}
			}
		}
		
		$form .= "</select>";
		
		$form .= "<input type=\"hidden\" name=\"statut\" value=\"".$statut."\" />";
		$form .= "<input type=\"hidden\" name=\"contenu\" value=\"".$contenu."\" />";
	
	}
	
	if ($mode == 2 or $mode == 4) {
		
		$res = mysql_query("SELECT * FROM Texte WHERE  id = '".$id_texte."' ;");
		
		while($row = mysql_fetch_array($res)) {
			
			if ($mode == 4) {
				$form .= " <script>
							$(function() {
								$( '#radio' ).buttonset();
							});
							</script>
				";
			}
			
			$form .= " <script type=\"text/javascript\">
					init_editeur();
					</script>
			";
			
			$form .= "<div class=\"center\">
						<br /><label for=\"contenu\">Contenu :</label><br /><br />
					</div>
					<div class=\"editeur\">
						<textarea id=\"afficheEditeur\" name=\"contenu\" >".$row['contenu']."</textarea>
					</div><br />
					
			";
			
			if ($mode == 2) {
			
				$form .= "<br /><label class=\"formInfos3\" for=\"statut\">Statut :</label>
						<select id=\"statut\" class=\"select3\" name=\"statut\" $disable>";
						
				if ($row['statut'] == "0") {
					$form .= "<option value=\"0\" selected=\"selected\">Non valider</option>
							<option value=\"1\">Privé</option>
							<option value=\"2\">Restreint</option>
							<option value=\"3\">Public</option>
							<option value=\"4\">Brouillon</option>
					";
				}
				if ($row['statut'] == "1") {
					$form .= "<option value=\"0\">Non valider</option>
							<option value=\"1\" selected=\"selected\">Privé</option>
							<option value=\"2\">Restreint</option>
							<option value=\"3\">Public</option>
							<option value=\"4\">Brouillon</option>
					";
				}
				if ($row['statut'] == "2") {
					$form .= "<option value=\"0\">Non valider</option>
							<option value=\"1\">Privé</option>
							<option value=\"2\" selected=\"selected\">Restreint</option>
							<option value=\"3\">Public</option>
							<option value=\"4\">Brouillon</option>
					";
				}
				if ($row['statut'] == "3") {
					$form .= "<option value=\"0\">Non valider</option>
							<option value=\"1\">Privé</option>
							<option value=\"2\">Restreint</option>
							<option value=\"3\" selected=\"selected\">Public</option>
							<option value=\"4\">Brouillon</option>
					";
				}
				if ($row['statut'] == "4") {
					$form .= "<option value=\"0\">Non valider</option>
							<option value=\"1\">Privé</option>
							<option value=\"2\">Restreint</option>
							<option value=\"3\">Public</option>
							<option value=\"4\" selected=\"selected\">Brouillon</option>
					";
				}

				$form .= "</select>";
			}
			
			if ($mode == 4) {
				
				// Si le texte est en attente de validation on ne peut changer le statut
				if ($row['statut'] == "0") {
					$form .= "<input type=\"hidden\" name=\"statut\" value=\"".$row['statut']."\" />";
				}
				else if ($row['statut'] == "4") { // Si le texte est un brouillon on peut le passer en attente de validation
					
					$form .= "<div id=\"radio\">
								<input type=\"radio\" id=\"radio1\" name=\"statut\" checked=\"checked\" /><label for=\"radio1\">Brouillon</label>
								<input type=\"radio\" id=\"radio2\" name=\"statut\" /><label for=\"radio2\">Publier</label>
							</div>
							
					";
					
				}
				else { // Si le texte est valide on peut changer entre privé, public et restreint
					
					if ($row['statut'] == "1") {
						$form .= "<div id=\"radio\">
									<input type=\"radio\" id=\"radio1\" name=\"statut\" value=\"1\" checked=\"checked\" /><label for=\"radio1\">Privé</label>
									<input type=\"radio\" id=\"radio2\" name=\"statut\" value=\"2\" /><label for=\"radio2\">Restreint</label>
									<input type=\"radio\" id=\"radio3\" name=\"statut\" value=\"3\" /><label for=\"radio3\">Public</label>
								</div>
								
						";
					}
					else if ($row['statut'] == "2") {
						$form .= "<div id=\"radio\">
									<input type=\"radio\" id=\"radio1\" name=\"statut\" value=\"1\" /><label for=\"radio1\">Privé</label>
									<input type=\"radio\" id=\"radio2\" name=\"statut\" value=\"2\" checked=\"checked\"/><label for=\"radio2\">Restreint</label>
									<input type=\"radio\" id=\"radio3\" name=\"statut\" value=\"3\" /><label for=\"radio3\">Public</label>
								</div>
								
						";
					}
					else {
						$form .= "<div id=\"radio\">
									<input type=\"radio\" id=\"radio1\" name=\"statut\" value=\"1\" /><label for=\"radio1\">Privé</label>
									<input type=\"radio\" id=\"radio2\" name=\"statut\" value=\"2\" /><label for=\"radio2\">Restreint</label>
									<input type=\"radio\" id=\"radio3\" name=\"statut\" value=\"3\" checked=\"checked\"/><label for=\"radio3\">Public</label>
								</div>
								
						";
					}
					
				}
			}
		
		}
		
		$form .= "<input type=\"hidden\" name=\"id_texte\" value=\"".$id_texte."\" />";

	}
	
	
	
	$form .= "<input type=\"hidden\" name=\"mode\" value=\"".$mode."\" />";
	
	$f->retourAjax["erreur"] = false;
	$f->retourAjax["msg"] = $form;
}

$f->endAjax();
?>